package com.easytopit.demo.spring.mvc.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class HandlerInterceptorNextDemo implements HandlerInterceptor {

	@Override // 当它返回为false 时，表示请求结束，后续的Interceptor 和Controller 都不会再执行；当返回值为true
				// 时就会继续调用下一个Interceptor 的preHandle 方法，如果已经是最后一个Interceptor
				// 的时候就会是调用当前请求的Controller 方法
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		System.out.printf("NEXT-preHandle:拦截 %s-> %s\n", request.getRequestURI(), handler);
		return true;
	}

	@Override // preHandle 返回值为true 时才能被调用, Controller调用后视图渲染前
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.printf("NEXT-postHandle:拦截 %s-> %s - %s\n", request.getRequestURI(), handler, modelAndView);
	}

	@Override // preHandle 返回值为true 时才能被调用, Controller调用后视图渲染后
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.printf("NEXT-afterCompletion:拦截 %s-> %s - %s\n", request.getRequestURI(), handler, ex);
	}

}
